package com.tianhui.consignortwofei.mvp.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.other.CommonEvent;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.ToastUtil;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.dialog.OperateTipDialog;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseDataListActivity;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.DriverModel;
import com.tianhui.consignortwofei.mvp.model.FuJianListBean;
import com.tianhui.consignortwofei.mvp.model.SendFuJianYuanBean;
import com.tianhui.consignortwofei.mvp.ui.adapter.FuJianListAdapter;
import com.tianhui.consignortwofei.other.EventWhat;
import com.tianhui.consignortwofei.utils.Util;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.inter.OnItemChildClickListener;
import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/8/10
 * @description
 */
public class FuJianListActivity extends BaseDataListActivity {


    @BindView(R.id.layout_search_editText)
    AutoCompleteTextView searchEditText;

    @BindView(R.id.ll_search_body)
    LinearLayout ll_search_body;


    private DriverModel driverModel;

    private String mSearchKey;
    private boolean firstGo=true;
    private int lookType=2;

    private SelectDialog mCargoRightsSelectDialog;
    private List<FuJianListBean.RecordsDTO> dataList =new ArrayList<>();
    private String phone="";

    @Override
    public int getLayoutId() {
        return R.layout.activity_fujian;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }


    @Override
    public RecyclerArrayAdapter createAdapter() {
//        return new QuotationDetailItemTwoAdapter(getContext(),dataList,lookType);

        return new FuJianListAdapter(getContext(),lookType);
    }

//    private void setResult(DriverInfo driverInfo) {
//        Intent intent = new Intent();
//        intent.putExtra(KeyValue.KEY_NAME, driverInfo.drivername);
//        intent.putExtra(KeyValue.KEY_PHONE, driverInfo.drivertel);
//        intent.putExtra(KeyValue.KEY_VEHICLE_NUMBER, driverInfo.defaultvehiclenum);
//        setResult(KeyValue.KEY_RESULT_CODE, intent);
//        finish();
//    }

    @Override
    public void initListener() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
//                goQuotationDetail(position);
                List<FuJianListBean.RecordsDTO> list =mAdapter.getAllData();
                String code = list.get(position).getCode();
                deleteDiaLog(position,code);

            }
        });

        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                List<FuJianListBean.RecordsDTO> list =mAdapter.getAllData();
                String code = list.get(position).getCode();
                deleteDiaLog(position,code);
//                switch (view.getId()){
//                    case R.id.tv_xiugai:
//                        goQuotationDetail(position);
//                        break;
//                    case R.id.tv_shanchu:
//                        deleteDiaLog(position);
//
//                        break;
//                }
            }
        });

    }






    @Override
    public void requestDataList(int page, boolean isDialog) {
        mSearchKey = searchEditText.getText().toString();
//        if (checkSearchKey(mSearchKey)) {

        requestDriverList(mSearchKey, isDialog,page);

//        } else {
//            stopRefresh();
//        }
    }

    @Override
    public void initData() {
        setTitle("销售订单未上传附件合同");


        driverModel=new DriverModel();

//        Bundle bundle = getIntent().getExtras();
//        lookType = bundle.getInt("lookType",2);
//        if (1==lookType){
//            setSubTitle("添加");
            ll_search_body.setVisibility(View.GONE);
//        }

//        setSubTitle("刷新列表");
//        setSubTitleColor(R.color.colorRed);
//        tv_hint.setVisibility(View.VISIBLE);

        searchEditText.setHint("请输入报价人");



    }



    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public void onSubtitleClick() {
//        Bundle bundle=new Bundle();
//        bundle.putInt("lookType",lookType);
//        openActivity(AddQuotationDetailActivity.class,bundle);
        searchDriverDetail();
    }



    @OnClick(R.id.layout_search_textView)
    public void searchDriverDetail() {
        mSearchKey = searchEditText.getText().toString();
//        if (checkSearchKey(mSearchKey)) {
            setRefresh(true);
            requestDriverList(mSearchKey, true,1);
//        }
    }




//    private boolean checkSearchKey(String searchKey) {
//        if (TextUtils.isEmpty(searchKey)) {
//            showEmptyView();
//        } else if (CheckUtil.isMobileNO(mSearchKey)) {
//            return true;
//        } else {
//            showToast("请输入正确的手机号");
//        }
//        return false;
//    }


    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);

        switch (EventWhat.valueOf(event.getWhat())) {
            case EVENT_REFRESH_BAOJIA_LIST:
                setRefresh(true);
                requestDataList(1,true);
                break;
        }
    }

    public void requestDriverList(String paihao, boolean isDialog,int page) {
        String consignorName = SharedPreferenceManager.getInstance().getConsignorName();
        String qianTian = Util.getQianTian();
        String qianNian = Util.getQianNian();
        Map<String, String> map = new HashMap<>();
        map.put("page", page+"");
        map.put("limit", "15");
        map.put("order", "desc");
        map.put("associate", "0");
//        map.put("personname", "周月");//名字孙占城
        map.put("personname", consignorName);//名字
        map.put("startDate", qianNian);
        map.put("endDate", qianTian);
//        map.put("reason", "0");//0未填写原因 其他，填写了


        driverModel.getUnFuJianList(this, map, isDialog, bindLifecycle(), new ObserverResponseListener<CommonResponse<FuJianListBean>>() {
            @Override
            public void onNext(CommonResponse<FuJianListBean> commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {

                        List<FuJianListBean.RecordsDTO> list = commonResponse.getData().getRecords();
//                        List<QuotationListBean.QuotationRecords> resultList = new ArrayList<>();

                        if (1==page){
                            dataList.clear();
                        }
                        dataList.addAll(list);

                        if (list != null && list.size() > 0) {
//                            setPageInfo(1, 1);
//                            for (DriverInfo info : list) {
//                                if (info.drivertel.equals(phone)) {
//                                    resultList.add(info);
//                                }
//                            }
//                            if (list.size() > 0) {
                                showDataList(list);
//                            } else {
//                                showEmptyView();
//                            }

                        }
                        else {
                            showEmptyView();
                        }

                    } else {
                        showToast(commonResponse.getMsg());
                    }
                }
            }


            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }



    public void deleteDiaLog(int position,String code) {

        new OperateTipDialog.Builder(this)
                .title("销售订单未上传附件合同原因")
                //                .content("存在多个牌号重量用逗号(,)隔开，调整数字即可，必须保留逗号。")
//                .input(fileName,new OperateTipDialog.InputCallback() {
//                    @Override
//                    public void onInput(@NonNull OperateTipDialog dialog, CharSequence input) {
//
//                    }
//                })
                .input(new OperateTipDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull OperateTipDialog dialog, CharSequence input) {
                    }
                })
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        String reason = dialog.getInputContent();
                        if (TextUtils.isEmpty(reason)) {
                            ToastUtil.showShortToast("原因不能为空");
                            return;
                        }
                        dialog.dismiss();

                        deleteQuotation(position,reason,code);
                    }
                })
                .show();

    }






    private void deleteQuotation(int position,String reason,String code) {
        SendFuJianYuanBean jianYuanBean=new SendFuJianYuanBean();
        jianYuanBean.setSaleDeliveryCode(code);
        jianYuanBean.setReason(reason);

        driverModel.sendFuJianYuan(getContext(), jianYuanBean, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    ToastUtil.showShortToast("添加成功");
                    mAdapter.remove(position);
                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });



//        Map<String, String> map = new HashMap<>();
//        map.put("id", driverInfo.getId()+"");
//        driverModel.deleteQuotation(getContext(), map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
//            @Override
//            public void onNext(CommonResponse commonResponse) {
//                if (commonResponse.getCode() == 20000) {
//                    showToast("删除成功");
//                    mAdapter.remove(position);
////                  quotationRecords.getPlasticsQuotationList().remove(position);
//                } else {
//                    showToast(commonResponse.getMsg());
//                }
//
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                showToast(e.message);
//            }
//        });

    }




    @Override
    protected void onDestroy() {
        super.onDestroy();

    }







}